/* eslint-disable @typescript-eslint/no-var-requires */
import { IBreadcrumb } from '@/base-ui/breadcrumb'
import { IMenuItem } from '@/components/nav-menu/types'
import type { RouteRecordRaw } from 'vue-router'
let firstMenu: any = null

export function mapMenuToRoutes(userMenu: any[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = []
  //先默认加载所有main的routes
  const allRoutes: RouteRecordRaw[] = []
  //获取所有路径再遍历 等于true查找router/main文件夹里面的所有xxxx.ts
  // const routeFiles = require.context('../router/main', true, /\.ts/)
  const routeFiles = import.meta.globEager('../router/main/**/*.ts')
  //使用require()加载对应文件
  for (const key in routeFiles) {
    if (Object.prototype.hasOwnProperty.call(routeFiles, key)) {
      const element = routeFiles[key]
      allRoutes.push(element.default)
    }
  }
  // Object.keys(routeFiles).forEach((key) => {
  //   const route = import.meta.glob('./' + key.split)
  //   console.log(route)
  //   // allRoutes.push(key.default)
  // })

  // const filePaths = routeFiles.keys().forEach((key) => {
  //   const route = require('../router/main' + key.split('.')[1])
  //   allRoutes.push(route.default)
  // })

  //根据userMenu获取需要的routes
  const _recurseGetRoute = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 2) {
        const route = allRoutes.find((route) => {
          return route.path === menu.url
        })
        if (route) {
          routes.push(route)
        }
        if (!firstMenu) firstMenu = menu
      } else {
        _recurseGetRoute(menu.children)
      }
    }
  }
  _recurseGetRoute(userMenu)
  return routes
}
//匹配面包屑
export function pathMapBreadCrumbs(
  userMenu: IMenuItem[],
  currentPath: string
): any {
  const breadcrumbs: IBreadcrumb[] = []
  pathMapMenu(userMenu, currentPath, breadcrumbs)
  return breadcrumbs
}
export function pathMapMenu(
  userMenu: IMenuItem[],
  currentPath: string,
  breadcrumbs?: IBreadcrumb[]
): any {
  for (const menu of userMenu) {
    if (menu.type === 1) {
      const findMenu = pathMapMenu(menu.children ?? [], currentPath)
      if (findMenu) {
        breadcrumbs?.push({ name: menu.name })
        breadcrumbs?.push({ name: findMenu.name })
        return findMenu
      }
    } else if (menu.type === 2 && menu.url === currentPath) {
      return menu
    }
  }
}
//动态配置权限按钮
//通过获取的Menus 递归遍历出三级权限然后返回权限数组 PageContent组件在使用的时候再次遍历然后v-if即可
export function mapMenusToPermission(userMenus: any[]) {
  //要返回的权限数组
  const permissions: string[] = []
  const _recurseGetPermission = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 1 || menu.type === 2) {
        _recurseGetPermission(menu.children ?? [])
      } else if (menu.type === 3) {
        permissions.push(menu.permission)
      }
    }
  }
  _recurseGetPermission(userMenus)
  return permissions
}

export function getMenuLeafNodeKeys(menuList: any[]) {
  const leafKeys: number[] = []
  const _recurseGetLeafNode = (menuList: any[]) => {
    for (const menu of menuList) {
      if (menu.children) {
        _recurseGetLeafNode(menu.children)
      } else {
        leafKeys.push(menu.id)
      }
    }
  }
  _recurseGetLeafNode(menuList)
  return leafKeys
}
export { firstMenu }
